<?php    
class ControllerProductManufacturer extends Controller { 
	private $error = array();
  
  	public function index() {
		$this->load->language('product/manufacturer');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/manufacturer');
    	$this->getList();
  	}
  
  	public function insert() {
		$this->load->language('product/manufacturer');
    	$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/manufacturer');
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_product_manufacturer->addManufacturer($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('product/manufacturer', 'token=' . $this->session->data['token'], 'SSL'));
		}
    	$this->getFormText();
    	$this->getInsertForm();
  	} 
   
  	public function update() {
		$this->load->language('product/manufacturer');
    	$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/manufacturer');
    	if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_product_manufacturer->editManufacturer($this->request->get['manufacturer_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('product/manufacturer', 'token=' . $this->session->data['token'], 'SSL'));
		}
    	$this->getFormText();
    	$this->getUpdateForm();
  	}
    
  	private function getList() {
  		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_display_name'] = $this->language->get('column_display_name');
		$this->data['column_authorize_desc'] = $this->language->get('column_authorize_desc');
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_action'] = $this->language->get('column_action');
		
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_filter'] = $this->language->get('button_filter');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/manufacturer', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		$this->data['insert'] = $this->url->link('product/manufacturer/insert', 'token=' . $this->session->data['token'], 'SSL');
		
		$url = '';
  		if (isset($this->request->get['filter_display_name'])) {
			$filter_display_name = $this->request->get['filter_display_name'];
			$url .= '&filter_display_name=' . $this->request->get['filter_display_name'];
		} else {
			$filter_display_name = "";
		}
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'm.name';
		}
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$this->data['manufacturers'] = array();
		$data = array(
			'filter_display_name'  => $filter_display_name,
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		$manufacturer_total = $this->model_product_manufacturer->getTotalManufacturers($data);
		$results = $this->model_product_manufacturer->getManufacturers($data);
    	foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('product/manufacturer/update', 'token=' . $this->session->data['token'] . '&manufacturer_id=' . $result['manufacturer_id'], 'SSL')
			);
			$this->data['manufacturers'][] = array(
				'manufacturer_id' => $result['manufacturer_id'],
				'name'            => $result['name'],
				'display_name'    => $result['display_name'],
				'sort_order'      => $result['sort_order'],
				'status'          => $result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),
				'action'          => $action
			);
		}
		
		$url_sort = $url;
		if ($order == 'ASC') {
			$url_sort .= '&order=DESC';
		} else {
			$url_sort .= '&order=ASC';
		}
		$this->data['sort_display_name'] = $this->url->link('product/manufacturer', 'token=' . $this->session->data['token'] . '&sort=md.display_name' . $url_sort, 'SSL');
		$this->data['sort_sort_order'] = $this->url->link('product/manufacturer', 'token=' . $this->session->data['token'] . '&sort=m.sort_order' . $url_sort, 'SSL');
		
  		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		$pagination = new Pagination();
		$pagination->total = $manufacturer_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('product/manufacturer', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
		$this->data['pagination'] = $pagination->render();
		
		$this->data['filter_display_name'] = $filter_display_name;
		$this->data['token'] = $this->session->data['token'];
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'product/manufacturer_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
  
  	private function getFormText() {
    	$this->data['heading_title'] = $this->language->get('heading_title');

    	$this->data['text_enabled'] = $this->language->get('text_enabled');
    	$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_default'] = $this->language->get('text_default');
    	$this->data['text_image_manager'] = $this->language->get('text_image_manager');
		$this->data['text_browse'] = $this->language->get('text_browse');
		$this->data['text_clear'] = $this->language->get('text_clear');			
		$this->data['text_percent'] = $this->language->get('text_percent');
		$this->data['text_amount'] = $this->language->get('text_amount');
				
		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_display_name'] = $this->language->get('entry_display_name');
    	$this->data['entry_image'] = $this->language->get('entry_image');
    	$this->data['entry_authorize_desc'] = $this->language->get('entry_authorize_desc');
    	$this->data['entry_authorize_image'] = $this->language->get('entry_authorize_image');
    	$this->data['entry_manufacturer_desc'] = $this->language->get('entry_manufacturer_desc');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		  
    	$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
 		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = '';
		}
  		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/manufacturer', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();					
		$this->data['token'] = $this->session->data['token'];
	}  
	 
	private function getInsertForm() {
    	$this->data['action'] = $this->url->link('product/manufacturer/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('product/manufacturer', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['manufacturer_description'] = array();
      	$this->data['name'] = '';
		$this->data['image'] = '';
		$this->data['authorize_image'] = '';
      	$this->data['sort_order'] = '';
		
		$this->load->model('tool/image');
		$this->data['thumb'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		$this->data['authorize_thumb'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		
		$this->template = 'product/manufacturer_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());
	}

	private function getUpdateForm() {
		$this->data['action'] = $this->url->link('product/manufacturer/update', 'token=' . $this->session->data['token'] . '&manufacturer_id=' . $this->request->get['manufacturer_id'], 'SSL');
		$this->data['cancel'] = $this->url->link('product/manufacturer', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->load->model('product/manufacturer');
		$this->load->model('tool/image');
		$manufacturer_info = $this->model_product_manufacturer->getManufacturer($this->request->get['manufacturer_id']);
		$this->data['name'] = $manufacturer_info['name'];
		$this->data['status'] = $manufacturer_info['status'];
		if ($manufacturer_info['image'] && file_exists(DIR_IMAGE . $manufacturer_info['image'])) {
			$this->data['image'] = $manufacturer_info['image'];
			$this->data['thumb'] = $this->model_tool_image->resize($manufacturer_info['image'], 100, 100);
		} else {
			$this->data['image'] = '';
			$this->data['thumb'] = '';
		}
		if ($manufacturer_info['authorize_image'] && file_exists(DIR_IMAGE . $manufacturer_info['authorize_image'])) {
			$this->data['authorize_image'] = $manufacturer_info['authorize_image'];
			$this->data['authorize_thumb'] = $this->model_tool_image->resize($manufacturer_info['authorize_image'], 100, 100);
		} else {
			$this->data['authorize_image'] = '';
			$this->data['authorize_thumb'] = '';
		}
		$this->data['sort_order'] = $manufacturer_info['sort_order'];
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		
		$manufacturer_descriptions = $this->model_product_manufacturer->getManufacturerDescription($this->request->get['manufacturer_id']);
		$this->data['manufacturer_description'] = array();
		foreach ($manufacturer_descriptions as $manufacturer_description) {
			$this->data['manufacturer_description'][$manufacturer_description['language_id']] = array(
				'manufacturer_id'     => $manufacturer_description['manufacturer_id'],
				'display_name'        => $manufacturer_description['display_name'],
				'authorize_desc'      => $manufacturer_description['authorize_desc'],
				'manufacturer_desc'   => $manufacturer_description['manufacturer_desc']
			);
		}
		
		$this->template = 'product/manufacturer_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
}
?>